import BaseConfig, { IItemConfig } from "../../../Script/Base/BaseConfig";
import BaseView from "../../../Script/Base/BaseView";
import { EBundleType, EResType, EViewType } from "../../../Script/Common/Enum";
import EventID from "../../../Script/Common/EventID";
import { GlobalData } from "../../../Script/Common/GlobalData";
import { IMessage, IViewData } from "../../../Script/Interface/IMessage";
import { SceneGame } from "../../../Script/SceneGame";
import AppService from "../../../Script/Service/AppService";
import GameUtil from "../../../Script/Util/GameUtil";
import { LoadUtil } from "../../../Script/Util/LoadUtil";

const { ccclass, property } = cc._decorator;

const VIEW_DATA: IViewData = {
    name: 'RewardUI',
    type: EViewType.Panel,
    bundle: EBundleType.Game,
    zIndex: 100,
};
@ccclass
export default class RewardUI extends BaseView implements IMessage {

    @property({ type: cc.Sprite, tooltip: "物品icon" })
    private icon: cc.Sprite = null;
    @property({ type: cc.Label, tooltip: "物品名字" })
    private labName: cc.Label = null;
    @property({ type: cc.Label, tooltip: "物品数量" })
    private labValue: cc.Label = null;
    @property({ type: cc.Label, tooltip: "物品介绍" })
    private labDesc: cc.Label = null;
    @property({ type: cc.Node, tooltip: "单倍领取" })
    private btnReward1: cc.Node = null;
    @property({ type: cc.Node, tooltip: "多倍领取" })
    private btnReward2: cc.Node = null;

    private data: { itemId: number, num: number } = null;
    private config: IItemConfig = null;

    recvMsg(cmd: number, data: any): void {
        switch (cmd) {
            case EventID.TEST_CMD:
                // this.refreshData();
                break;
        }
    }

    protected prepare(data: any) {
        // console.log("BaseView.prepare()===>data:", data);
        this.data = data;
    }

    protected onLoad() {
        super.onLoad();
        this.refreshUI();
    }

    private refreshUI() {
        this.config = BaseConfig.instance.getItemInfoById(this.data.itemId);
        if (this.config) {
            this.labValue.string = `${this.data.num}`;
            this.labValue.string = `${this.data.num}`;
            this.labName.string = `${this.config.name}`;
            this.labDesc.string = `价值:${this.config.basePrice}`;
            this.btnReward2.active = false;

            // icon
            LoadUtil.instance.setSpriteFrame(this.icon, EResType.Item, this.config.id, 100, 100);
        }
    }

    private getReward(ratio: number) {
        this.onCloseClicked();
        // 添加到背包中
        GlobalData.addBagData(this.config.id, this.data.num);

        SceneGame.instance.showToast(cc.v2(0, 0), '物品已放入背包');
    }

    private onOneClicked(event: cc.Event.EventTouch, data: string) {
        this.getReward(1);
    }

    private onRatioClicked(event: cc.Event.EventTouch, data: string) {
        this.getReward(2);
    }
}
AppService.instance.registViewData(VIEW_DATA);